﻿namespace LaoLuo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Timers;

    /// <summary>
    /// timer that times just one time
    /// </summary>
    public class TimerOneTime
    {
        /// <summary>
        /// call a action when time is right
        /// </summary>
        /// <param name="callback">action that will be called</param>
        /// <param name="time">time to call the action</param>
        public static void WorkOnTime(Action callback, DateTime time)
        {
            var alarmTimer = new Timer((time - DateTime.Now).TotalMilliseconds) { AutoReset = false };
            alarmTimer.Elapsed += delegate
                                      {
                                          callback();
                                          alarmTimer.Close();
                                          GC.ReRegisterForFinalize(alarmTimer);
                                      };

            alarmTimer.Start();
            GC.SuppressFinalize(alarmTimer);
        }
    }
}
